package com.songlanyun.modules.shop.controller.admin;

import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.shop.entity.UnionBusinessClassifyEntity;
import com.songlanyun.modules.shop.model.dto.UnionBusinessClassifyDTO;
import com.songlanyun.modules.shop.model.dto.UnionBusinessRelationDTO;
import com.songlanyun.modules.shop.service.UnionBusinessClassifyService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 联盟商家分类
 *
 * @author WuChao
 * @email songlanyun@gmail.com
 * @date 2022-11-21 14:38:04
 */
@RestController
@RequestMapping("admin/union/business/classify")
public class UnionBusinessClassifyAdminController extends AbstractController {
    @Autowired
    private UnionBusinessClassifyService unionBusinessClassifyService;

    /**
     * 分页列表
     */
    @GetMapping("/page")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = unionBusinessClassifyService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list() {
        List<UnionBusinessClassifyEntity> list = unionBusinessClassifyService.lambdaQuery()
                .orderByAsc(UnionBusinessClassifyEntity::getRank)
                .orderByDesc(UnionBusinessClassifyEntity::getCreateTime)
                .list();
        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        UnionBusinessClassifyEntity unionBusinessClassify = unionBusinessClassifyService.getById(id, true);
        return R.ok().put("data", unionBusinessClassify);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody UnionBusinessClassifyDTO dto) {
        ValidatorUtils.validateEntity(dto, AddGroup.class);
        unionBusinessClassifyService.add(getUserId(), dto);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody UnionBusinessClassifyDTO dto) {
        ValidatorUtils.validateEntity(dto, UpdateGroup.class);
        unionBusinessClassifyService.edit(getUserId(), dto);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        unionBusinessClassifyService.delete(id);
        return R.ok();
    }

    /**
     * 设置联盟商家分类
     */
    @SysLog("设置联盟商家分类")
    @PostMapping("/set/classify")
    public R setClassify(@RequestBody UnionBusinessRelationDTO dto) {
        ValidatorUtils.validateEntity(dto);
        verifyAdmin();
        unionBusinessClassifyService.setClassify(dto);
        return R.ok();
    }

    /**
     * 显示隐藏
     */
    @SysLog("联盟商家分类显示隐藏")
    @PostMapping("/show/{id}")
    public R show(@PathVariable("id") Long id) {
        unionBusinessClassifyService.show(id);
        return R.ok();
    }
}
